package cn.sanch.system.controller;

import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.PageList;
import cn.sanch.system.domain.SystemDictionaryDetail;
import cn.sanch.system.query.SystemDictionaryDetailQuery;
import cn.sanch.system.service.ISystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：数据字典明细
 * @author sanch37
 * 2022/5/28
 */
@Api(value = "宠物之家的数据字典明细相关API", description = "数据字典明细的CRUD操作")
@RestController
@RequestMapping("/systemDictionaryDetail")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 接口：查询数据字典明细单个对象
     * @param id Long id
     * @return SystemDictionaryDetail
     */
    @ApiOperation(value = "查询数据字典明细单个对象")
    @GetMapping("/{id}")
    public SystemDictionaryDetail queryOne(@PathVariable Long id) {
        return systemDictionaryDetailService.findOne(id);
    }


    /**
     * 接口：查询数据字典明细所有对象
     * @return List<SystemDictionaryDetail>
     */
    @ApiOperation(value = "查询数据字典明细所有对象")
    @GetMapping
    public List<SystemDictionaryDetail> queryAll() {
        return systemDictionaryDetailService.findAll();
    }


    /**
     * 接口：删除单个数据字典明细对象
     * @param id Long id
     * @return AjaxResult
     */
    @ApiOperation(value = "删除单个数据字典明细对象")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            systemDictionaryDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！");
        }
    }

    /**
     * 接口：高级查询 + 分页查询
     * @param systemDictionaryDetailQuery SystemDictionaryDetailQuery systemDictionaryDetailQuery
     * @return PageList<SystemDictionaryDetail>
     */
    @ApiOperation(value = "高级查询 + 分页查询")
    @PostMapping
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery) {
        return systemDictionaryDetailService.queryPage(systemDictionaryDetailQuery);
    }

    /**
     * 接口：数据字典明细添加或修改
     * @param systemDictionaryDetail
     * @return
     */
    @ApiOperation(value = "数据字典明细添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        try {
            /* 没有id为添加 */
            if (systemDictionaryDetail.getId() == null) {
                systemDictionaryDetailService.save(systemDictionaryDetail);
            /* 有id为修改 */
            }else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口：批量删除数据字典明细
     * @param ids Long[] ids
     * @return AjaxResult
     *
     *
     */
    @ApiOperation(value = "批量删除数据字典明细")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            systemDictionaryDetailService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口：根据前端传过来的type_id来查询对应的对象
     * @param types_id
     * @return
     */
    @GetMapping("/findBy/{types_id}")
    public List<SystemDictionaryDetail> findByTypeId(@PathVariable Long types_id) {

        return systemDictionaryDetailService.findByTypeId(types_id);
    }

}
